package com.example.demo.controller;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLClassLoader;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <h3>springboot_upload_download</h3>
 * <p></p>
 *
 * @author : 你的名字
 * @date : 2020-03-19 18:10
 **/
@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("文件名：" + file.getOriginalFilename());
        System.out.println("文件类型：" + file.getContentType());
        System.out.println("文件大小：" + file.getSize());

        String filename = file.getOriginalFilename();
        String extensionName = filename.substring(filename.lastIndexOf("."));
        System.out.println(extensionName);

        //根据相对路径获取绝对路径
        String path = request.getSession().getServletContext().getRealPath("/files");
        //创建时间文件夹
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File file1 = new File(path, format);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        //获取文件后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") +
                new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String newFileName = newFileNamePrefix + "." + extension;

        //处理上传操作
        file.transferTo(new File(path, newFileName));
        return "redirect:/upload.jsp";
    }

    @RequestMapping("download")
    public void downLoad(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //根据相对路径获取绝对路径
        String path = request.getSession().getServletContext()
                .getRealPath("/files/download");
        //获取文件名，根据文件名去指定的目录读取文件
        FileInputStream is = new FileInputStream(new File(path, fileName));
        //设置下载响应头
        response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        //通过响应流响应
        ServletOutputStream outputStream = response.getOutputStream();
        //流的复制
        IOUtils.copy(is, outputStream);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(outputStream);
    }
}
